/**
 *  目录  深度优先 + 先序遍历 123 213
 */
const fs = require("fs");
const path = require("path");
//先序深度优先
function preDeep(dir, cb) {
  console.log(dir);
  fs.readdir(dir, (err, files) => {
    !(function next(i) {
      if (i >= files.length) return cb();
      let child = path.join(dir, files[i]);
      fs.stat(child, (err, stat) => {
        if (stat.isDirectory()) {
          preDeep(child, () => next(i + 1));
        } else {
          console.log(child);
          next(i + 1);
        }
      });
    })(0);
  });
}
// preDeep("a", () => {
//   console.log("over");
// });

// wide 广度优先 同步异步文件处理
function wide(dir) {
  let arr = [dir];
  while (arr.length) {
    let current = arr.shift();
    console.log(current);
    let stat = fs.statSync(current);
    if (stat.isDirectory()) {
      let files = fs.readdirSync(current);
      files.forEach((i) => {
        arr.push(path.join(current,i));
      });
    } 
  }
}
wide("a");
//读文件 写文件 文件夹 同步 异步 ，，， 异步回调 promsie all 
